#include "stdafx.h"
#include "util.h"
#include <Windows.h>
int setAutoStart()
{
	long res;
	HKEY key, hRoot;
	DWORD state;
	char path[_MAX_PATH];
	int ok;
	OSVERSIONINFO version;
	version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	GetVersionEx(&version);
	if (version.dwPlatformId == VER_PLATFORM_WIN32_NT)
		hRoot = HKEY_CURRENT_USER;
	else
		hRoot = HKEY_LOCAL_MACHINE;

/*
	res = RegOpenKeyEx(
				hRoot, 
				_T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
				0, // option, not used
				KEY_SET_VALUE | KEY_QUERY_VALUE, // allow setting values
				&key);
*/
	res = RegCreateKeyEx(
				hRoot,
				"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
				0,
				NULL,
				REG_OPTION_NON_VOLATILE,    // special options flag
				KEY_QUERY_VALUE | KEY_SET_VALUE,        // desired security access
				NULL,
				&key,          // receives opened handle
				&state);

	if (res!=ERROR_SUCCESS)
		return 0;

	//if (enabled) {
		GetModuleFileName(NULL, path, sizeof(path)/sizeof(char));
		res = RegSetValueEx(key, "horo", 0, REG_SZ, (LPBYTE)path, (strlen(path)+1)*sizeof(char));
		ok = (res == ERROR_SUCCESS);
	//}
	/*else {
		RegDeleteValue(key, STARTUP_REG_VALUENAME);
		res = RegQueryValueEx(key, STARTUP_REG_VALUENAME, 0, NULL, NULL, NULL);
		ok = (res != ERROR_SUCCESS);
	}*/
	RegCloseKey(key);
	return ok;
}